db = db.getSiblingDB("pcloud_core")
var _now_time = Long(ISODate().valueOf() / 1000)
db.pcloud_users.insertOne({"username":"root","nickname":"超管","phone":"","email":"","password":"root.2020","portrait":"","roles":"Admin","contacts":"{}","maintainer":0,"create_at":_now_time,"create_by":"system","update_at":_now_time,"update_by":"system"})
db.user_group.insertOne({"name":"超管组","note":"","create_at":_now_time,"create_by":"root","update_at":_now_time,"update_by":"root"})

//group_id 来源于user_group
//user_id 来源于 pcloud_users
var group_id = db.user_group.findOne({"name":"超管组"})._id.toString()
var user_id = db.pcloud_users.findOne({"username":"root"})._id.toString()
db.user_group_member.insertOne({"group_id":group_id,"user_id":user_id})
db.busi_group.insertOne({"name":"Default Busi Group","label_enable":0,"label_value":"","create_at":_now_time,"create_by":"root","update_at":_now_time,"update_by":"root"})

//gourp_id 来源于user_groups
//user_group_id 来源于user_group
db.busi_group_member.insertOne({"busi_group_id":group_id,"user_group_id":user_id,"perm_flag":"rw"})

db.role.insertOne({"name":"Admin",    "note":"Administrator role"})
db.role.insertOne({"name":"Standard", "note":"Ordinary user role"})
db.role.insertOne({"name":"Readonly", "note":"Readonly user role"})


db.role_operation.insertOne({"role_name":"Guest","operation":"/metric/explorer"})
db.role_operation.insertOne({"role_name":"Guest","operation":"/object/explorer"})
db.role_operation.insertOne({"role_name":"Guest","operation":"/log/explorer"})
db.role_operation.insertOne({"role_name":"Guest","operation":"/trace/explorer"})
db.role_operation.insertOne({"role_name":"Guest","operation":"/help/version"})
db.role_operation.insertOne({"role_name":"Guest","operation":"/help/contact"})

db.role_operation.insertOne({"role_name":"Standard","operation":"/metric/explorer"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/object/explorer"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/log/explorer"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/trace/explorer"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/help/version"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/help/contact"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/help/servers"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/help/migrate"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-rules-built-in"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/dashboards-built-in"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/trace/dependencies"})

db.role_operation.insertOne({"role_name":"Admin","operation":"/help/source"})
db.role_operation.insertOne({"role_name":"Admin","operation":"/help/sso"})
db.role_operation.insertOne({"role_name":"Admin","operation":"/help/notification-tpls"})
db.role_operation.insertOne({"role_name":"Admin","operation":"/help/notification-settings"})

db.role_operation.insertOne({"role_name":"Standard","operation":"/users"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/user-groups"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/user-groups/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/user-groups/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/user-groups/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/busi-groups"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/busi-groups/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/busi-groups/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/busi-groups/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/targets"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/targets/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/targets/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/targets/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/dashboards"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/dashboards/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/dashboards/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/dashboards/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-rules"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-rules/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-rules/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-rules/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-mutes"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-mutes/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-mutes/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-subscribes"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-subscribes/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-subscribes/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-subscribes/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-cur-events"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-cur-events/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/alert-his-events"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tpls"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tpls/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tpls/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tpls/del"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tasks"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tasks/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/job-tasks/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/recording-rules"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/recording-rules/add"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/recording-rules/put"})
db.role_operation.insertOne({"role_name":"Standard","operation":"/recording-rules/del"})


db.metric_view.insertOne({"name":"Host View", "cate":0,"configs":'{"filters":[{"oper":"=","label":"__name__","value":"cpu_usage_idle"}],"dynamicLabels":[],"dimensionLabels":[{"label":"ident","value":""}]}'})

db.alert_aggr_view.insertOne({"name":"By BusiGroup, Severity","rule":"field:group_name::field:severity","cate":0})
db.alert_aggr_view.insertOne({"name":"By RuleName","rule":"field:rule_name","cate":0})


// indexes
db.getSiblingDB("pcloud_core").getCollection("pcloud_users").createIndex({"username": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("user_group").createIndex({"create_by": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("user_group").createIndex({"update_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("user_group_member").createIndex({"group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("user_group_member").createIndex({"user_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("role").createIndex({"name": 1},{unique: true, background: true})

// db.getSiblingDB("pcloud_core").getCollection("configs").createIndex({"ckey": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("role_operation").createIndex({"role_name": 1, "operation": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("busi_group").createIndex({"name": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("busi_group_member").createIndex({"busi_group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("busi_group_member").createIndex({"user_group_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("board").createIndex({"ident": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("board").createIndex({"group_id": 1, "name": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("board_payload").createIndex({"board_id": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("chart_share").createIndex({"create_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("alert_rule").createIndex({"group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("alert_rule").createIndex({"update_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("alert_mute").createIndex({"group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("alert_mute").createIndex({"update_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("alert_subscribe").createIndex({"group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("alert_subscribe").createIndex({"update_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("target").createIndex({"ident": 1},{unique: true, background: true})
db.getSiblingDB("pcloud_core").getCollection("target").createIndex({"group_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("metric_view").createIndex({"create_by": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("recording_rule").createIndex({"group_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("recording_rule").createIndex({"update_at": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("alert_aggr_view").createIndex({"create_by": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("alert_his_event").createIndex({"hash": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("alert_his_event").createIndex({"rule_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("alert_his_event").createIndex({"trigger_time": 1, "group_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_tpl").createIndex({"group_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_tpl_host").createIndex({"task_tpl_id": 1, "host": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_record").createIndex({"create_by": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("task_record").createIndex({"event_id": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("task_record").createIndex({"create_at": 1, "group_id": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("datasource").createIndex({"name": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("notify_tpl").createIndex({"channel": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("sso_config").createIndex({"name": 1},{unique: true, background: true})

db.getSiblingDB("pcloud_core").getCollection("es_index_pattern").createIndex({"datasource_id": 1, "name": 1},{unique: true, background: true})

//
db.getSiblingDB("pcloud_core").getCollection("task_meta").createIndex({"creator": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("task_meta").createIndex({"created": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_scheduler").createIndex({"_id": 1, "scheduler": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_scheduler_health").createIndex({"scheduler": 1},{unique: true, background: true})
db.getSiblingDB("pcloud_core").getCollection("task_scheduler_health").createIndex({"clock": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("task_host_doing").createIndex({"host": 1},{background: true})
// task_host_XX

// 
db.getSiblingDB("pcloud_core").getCollection("alert_his_event").createIndex({"last_eval_time": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("svc_mongo_mgr").createIndex({"mongo_db_cluster_name": 1},{background: true})

db.getSiblingDB("pcloud_core").getCollection("hb_mongodb").createIndex({"labels.cluster": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("hb_mongodb").createIndex({"labels.instance": 1},{background: true})
// db.getSiblingDB("pcloud_core").getCollection("hb_mongodb").createIndex({"sampletime": 1},{background: true, expireAfterSeconds: XXXXXXX})

db.getSiblingDB("pcloud_core").getCollection("mongo_current_op").createIndex({"mongo_id": 1, "host": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("mongo_current_op").createIndex({"mongo_id": 1, "current_op_time": 1},{background: true})
// db.getSiblingDB("pcloud_core").getCollection("mongo_current_op").createIndex({"current_op_time": 1},{background: true, expireAfterSeconds: XXXXXXX})

db.getSiblingDB("pcloud_core").getCollection("mongo_slow_log").createIndex({"mongo_id": 1, "node": 1},{background: true})
db.getSiblingDB("pcloud_core").getCollection("mongo_slow_log").createIndex({"mongo_id": 1, "ts": 1},{background: true})
// db.getSiblingDB("pcloud_core").getCollection("mongo_slow_log").createIndex({"ts": 1},{background: true, expireAfterSeconds: XXXXXXX})

